package com.henu.yizhiya.util;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.henu.R;
import com.henu.yizhiya.db.RoomDB;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by y on 2017/5/19.
 */

public class BuildListAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<List<RoomDB>> list = new ArrayList<>();
    private LayoutInflater inflater;

    //构造方法
    public BuildListAdapter(Context context) {
        this.context= context;
        inflater = LayoutInflater.from(context);
    }

    public ArrayList<List<RoomDB>> getListAll(){
        return list;
    }
    //为适配器提供数据的方法
    public void addAllData(ArrayList<RoomDB> tempList){
        Map<String,List<RoomDB>> map = new HashMap<>();
        List<RoomDB> r_List;
        for (RoomDB r : tempList) {
            String k = r.getR_num().charAt(0)+"";
            if (map.containsKey(k)) {
                r_List = map.get(k);
                r_List.add(r);

            } else {
                r_List = new ArrayList<>();
                r_List.add(r);
                map.put(k, r_List);
            }
        }
        for (Map.Entry<String, List<RoomDB>> item : map.entrySet()) {
            list.add(item.getValue());
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public List<RoomDB> getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewBuildHolder holder;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.build_items,null);
            holder = new ViewBuildHolder();

            holder.tv_build = (TextView) convertView.findViewById(R.id.tv_build);
            convertView.setTag(holder);
        }else {
            holder = (ViewBuildHolder) convertView.getTag();
        }

        final List<RoomDB> rooms = list.get(position);

        String i = rooms.get(position).getR_num().charAt(0)+"号楼";
        holder.tv_build.setText(i);
        return convertView;

    }

    class ViewBuildHolder{
        TextView tv_build=null;
    }

}
